/*******************************************************************************
**  Player / Player and Player / windows boundry collisions are not currently **
**  implemented, so you can walk off the end of the screen.                   **
********************************************************************************/

#include "include.h"
using namespace irr;
using namespace std;

bool Game::runGame()
{
    // array for input buffer
    bool keys[irr::KEY_KEY_CODES_COUNT];
    // input class, listens for key presses
    InputReceiver listener(keys);

    // set the video device
    IrrlichtDevice* device = irr::createDevice(irr::video::EDT_OPENGL,
       irr::core::dimension2d<s32>(SCREEN_W,SCREEN_H), 16, false, false, false, &listener);

    // couldn't load driver, so bail out
    if (device == 0) return 1;

    // window titlebar text
    device->setWindowCaption(L"Spike vs. Mothma BETA");
    // set video driver
    video::IVideoDriver* driver = device->getVideoDriver();

    // create graphics objects
    Graphics display(device, driver);

    // create objects to be displayed
    DrawableObj back;
    Player mothma(10,100, P1_W, P1_H);
    Player spike(800,200, P2_W, P2_H);
    Projectile note(mothma.getXPosition()+P1_PROJ_W+10,300, P1_PROJ_W, P1_PROJ_H);
    Projectile spikeProj(spike.getXPosition()+P2_PROJ_W+200,270, P2_PROJ_W, P2_PROJ_H);
    DrawableObj winMothma;
    DrawableObj winSpike;
    // add frames to each object.
    back.addFrames(1,"img/background.png", 0, SCREEN_W, SCREEN_H);
    mothma.addFrames(1,"img/mothma_stand.png",0, P1_W, P1_H);
    spike.addFrames(1,"img/spike1.png", 0, P2_W, P2_H);
    note.addFrames(1,"img/note.png", 0, 32, 32);
    spikeProj.addFrames(1,"img/spikeproj.png", 0, 64, 32);
    winMothma.addFrames(1,"img/win-mothma.png", 0, 800, 600);
    winSpike.addFrames(1,"img/win-spike.png", 0, 800, 600);

    bool gameOver = false;
    // main game loop
    while (device->run() && driver)
    {
        // if the game isn't over
        if(gameOver == false)
        {
            // player input
            if(keys[irr::KEY_DOWN])
                spikeProj.setIsVisible(true);
            if(keys[irr::KEY_RIGHT] && !Collision::checkBoundryCollision(spike, back,10))
            {
                spike.moveObject(X_MOV,0);
            }
            if(keys[irr::KEY_LEFT] && !Collision::checkCollision(mothma,spike,10))
            {
                spike.moveObject(-X_MOV,0);
            }

            if(keys[irr::KEY_KEY_S])
                note.setIsVisible(true);
            if(keys[irr::KEY_KEY_D] && !Collision::checkCollision(mothma,spike,10))
            {
                mothma.moveObject(X_MOV,0);
            }
            if(keys[irr::KEY_KEY_A] && !Collision::checkBoundryCollision(mothma, back,10))
                mothma.moveObject(-X_MOV,0);

            driver->beginScene(true, false, video::SColor(255, 0, 0, 0));
            // draw a static background
            display.draw(back.getFrameVector(),0,0,0);
            // draw spikes projectile
            // if it hits a window boundry, make projectile go away and reset the position
            // if it hits a player, make projectile go away, reset the position and decrement
            // opponents hit points
            if(spikeProj.getIsVisible() == true)
            {
                spikeProj.moveObject(-2,0);
                display.draw(spikeProj.getFrameVector(),spikeProj.getXPosition(),spikeProj.getYPosition(),0);
                if(Collision::checkBoundryCollision(spikeProj,back) == false)
                {    if(Collision::checkCollision(mothma,spikeProj) == true)
                    {
                        spikeProj.setIsVisible(false);
                        spikeProj.setCoords(spike.getXPosition(),270);
                        mothma.decrementHitPoints();
                    }
                }
                else
                {
                    spikeProj.setIsVisible(false);
                    spikeProj.setCoords(spike.getXPosition(),270);
                }
            }
            // draw mothmas projectile
            if(note.getIsVisible() == true)
            {
                note.moveObject(2,0);
                display.draw(note.getFrameVector(),note.getXPosition(),note.getYPosition(),0);
                if(Collision::checkBoundryCollision(note,back) == false)
                {    if(Collision::checkCollision(spike,note) == true)
                    {
                        note.setIsVisible(false);
                        note.setCoords(mothma.getXPosition(),270);
                        spike.decrementHitPoints();
                    }
                }
                else
                {
                    note.setIsVisible(false);
                    note.setCoords(mothma.getXPosition(),270);
                }
            }

            // draw two player sprites
            display.draw(mothma.getFrameVector(),mothma.getXPosition(),mothma.getYPosition(),0);
            display.draw(spike.getFrameVector(),spike.getXPosition(),spike.getYPosition(),0);
            // check if either player is dead, if either is, display which player wins
            // and set gameOver so the game stops.
            if(mothma.getHitPoints() <= 0)
            {
                display.draw(winSpike.getFrameVector(),100,0,0);
                gameOver = true;
            }
            if(spike.getHitPoints() <= 0)
            {
                display.draw(winMothma.getFrameVector(),100,0,0);
                gameOver = true;
            }

            driver->endScene();
        }
    }

    device->drop();
    return true;
}


